package web.selvlet;

import pojo.*;
import service.*;
import service.impl.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    private UserService us = new UserServiceImpl();
//    private EmpService es = new EmpServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取表单的用户名和密码
        String username = req.getParameter("username");
        String pwd = req.getParameter("pwd");
        System.out.println("username = " + username);
        System.out.println("pwd = " + pwd);
        //调用业务逻辑层登录方法
        User user = new User();
        user.setUsername(username);
        user.setPassword(pwd);
        User user1 = us.userLogin(user);
        if(user1!=null){
            //跳转主页
            //重定向跳转页面 (特点：跳转资源页面，但是不携带数据)
//            resp.sendRedirect("main.html");

            //获取员工列表数据  调用业务逻辑层查询所有员工的方法
//            List<Emp> allEmps = es.findAllEmps();
            //把数据存储到request域中
//            req.setAttribute("emps",allEmps);
            //请求转发 跳转页面 (特点：携带数据进行页面跳转)
//            req.getRequestDispatcher("/page/emps.jsp").forward(req,resp);
            resp.sendRedirect("empHandle?op=queryAll");
        }else{
            resp.sendRedirect("err.html");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
